package com.zy.study.utils;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;

/*
@JsonSerialize注解,主要用于数据转换,该注解作用在该属性的getter()方法上。
使用jackson进行序列化时，往往会遇到后台某个实体对象的属性为null，当序列化成json时对应的属性也为null；这样在某些前端组件上应用该json对象会报错。
在实体类前，增加@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 注解
JSON原来经过JACKSON转换以后为{"name"："name","sex":null}
加入注解后，结果为{"name"："name"}
sex节点被去掉了
 */
@Data
public class ServiceResponse<T> implements Serializable {

	/**
	* 
	*/
	private static final long serialVersionUID = 1L;
	private int code;
	private String msg;
	private int count;
	private T data;

	private ServiceResponse(int code) {
		this.code = code;
	}

	private ServiceResponse(int code, T data) {
		this.code = code;
		this.data = data;
	}

	private ServiceResponse(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public ServiceResponse(int code, String msg, int count, T data) {
		this.code = code;
		this.msg = msg;
		this.count = count;
		this.data = data;
	}

	@JsonIgnore
	// 使之不在序列化当中
	public boolean isSuccess() {
		return this.code == ResponseCode.SUCCESS.getCode();
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	// 成功返回status
	public static <T> ServiceResponse<T> createBySuccess() {
		return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode());
	}

	// 成功返回status和msg
	public static <T> ServiceResponse<T> createBySuccessMessage(String msg) {
		return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
	}

	// 成功返回status和data
	public static <T> ServiceResponse<T> createBySuccess(T data) {
		return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), data);
	}

	// 成功返回status，msg和data
	public static <T> ServiceResponse<T> createBySuccess(String msg, int count, T data) {
		return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), msg, count, data);
	}

	/**
	 * 处理失败
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByFailed() {
		return new ServiceResponse<T>(ResponseCode.FAILED.getCode(), ResponseCode.FAILED.getDesc());
	}
	/**
	 * 未登录
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByNOT_loginMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.NOT_LOGIN.getCode(), failedMessage);
	}
	
	/**
	 * 未激活
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByNOT_activedMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.NOT_ACTIVED.getCode(), failedMessage);
	}
	
	/**
	 * 访问拒绝
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByACCESS_deniedMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.ACCESS_DENIED.getCode(), failedMessage);
	}
	
	/**
	 * 数据库错误
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByDB_errorMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.DB_ERROR.getCode(), failedMessage);
	}
	
	/**
	 * 参数错误
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByPARAM_parameter_errorMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.PARAM_PARAMETER_ERROR.getCode(),
				failedMessage);
	}
	
	/**
	 * 参数为空
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByPARAM_parameter_is_nullMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.PARAM_PARAMETER_IS_NULL.getCode(),failedMessage);
	}
	/**
	 * 自定义错误
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByError() {
		return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
	}
	/**
	 * 自定义错误
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByErrorMessage(String errorMessage) {
		return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
	}
	/**
	 * 自定义错误
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
		return new ServiceResponse<T>(errorCode, errorMessage);
	}


	/**
	 * 处理失败
	 * @param <T>
	 * @param failedMessage
	 * @return
	 */
	public static <T> ServiceResponse<T> createByFailedMessage(String failedMessage) {
		return new ServiceResponse<T>(ResponseCode.FAILED.getCode(), failedMessage);
	}
	/**
	 * 未登录
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByNOT_login() {
		return new ServiceResponse<T>(ResponseCode.NOT_LOGIN.getCode(), ResponseCode.NOT_LOGIN.getDesc());
	}
	
	/**
	 * 未激活
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByNOT_actived() {
		return new ServiceResponse<T>(ResponseCode.NOT_ACTIVED.getCode(), ResponseCode.NOT_ACTIVED.getDesc());
	}
	
	/**
	 * 访问拒绝
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByACCESS_denied() {
		return new ServiceResponse<T>(ResponseCode.ACCESS_DENIED.getCode(), ResponseCode.ACCESS_DENIED.getDesc());
	}
	
	/**
	 * 数据库错误
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByDB_error() {
		return new ServiceResponse<T>(ResponseCode.DB_ERROR.getCode(), ResponseCode.DB_ERROR.getDesc());
	}
	
	/**
	 * 参数错误
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByPARAM_parameter_error() {
		return new ServiceResponse<T>(ResponseCode.PARAM_PARAMETER_ERROR.getCode(),
				ResponseCode.PARAM_PARAMETER_ERROR.getDesc());
	}
	
	/**
	 * 参数为空
	 * @param <T>
	 * @return
	 */
	public static <T> ServiceResponse<T> createByPARAM_parameter_is_null() {
		return new ServiceResponse<T>(ResponseCode.PARAM_PARAMETER_IS_NULL.getCode(),
				ResponseCode.PARAM_PARAMETER_IS_NULL.getDesc());
	}



}
